package com.example.myapplication.ui.screens


import android.widget.VideoView
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Star
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.module.webview.WebView
import com.example.module.webview.rememberWebViewState
import com.example.myapplication.ViewModel.VideoViewModel
import com.example.myapplication.ui.components.video.rememberVodController
import com.example.myapplication.ui.components.video.videoView
import com.google.accompanist.insets.statusBarsPadding
import com.tencent.rtmp.TXVodPlayer
import kotlinx.coroutines.launch

@Composable
fun VideoDetailScreen(videoViewModel: VideoViewModel = viewModel(), onBack: () -> Unit) {

    val webViewState = rememberWebViewState(data = videoViewModel.videoDesc)
//    val vodPlayer = TXVodPlayer(LocalContext.current)
    val vodController = rememberVodController()
    LaunchedEffect(vodController) {
        // https://klxxcdn.oss-cn-hangzhou.aliyuncs.com/histudy/hrm/media/bg1.mp4
        vodController.startPlay(videoViewModel.videoUrl)
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    Text(
                        text = "文章详情",
                        color = Color.White,
                        fontSize = 18.sp
                    )
                },
                navigationIcon = {
                    Icon(
                        imageVector = Icons.Default.Close,
                        contentDescription = null,
                        modifier = Modifier
                            .clickable {
                                onBack()
                            }
                            .padding(start = 15.dp))
                }
            )
        },
        modifier = Modifier
            .background(MaterialTheme.colors.primary)
            .statusBarsPadding(),
    ) {
        Column(modifier = Modifier.fillMaxWidth()) {
            // 视频区域
            Box(modifier = Modifier.height(200.dp)) {
                videoView(vodPlayer = vodController.vodPlayer)
            }
            // 简介区域test
            WebView(state = webViewState)
        }
    }
}

